package com.only4play.portal.cp.config.impl;

import com.only4play.portal.common.bean.AccessToken;
import com.only4play.portal.common.util.http.apache.ApacheHttpClientBuilder;
import com.only4play.portal.cp.config.CpConfigStorage;
import com.only4play.portal.cp.constant.CpApiPathConstants;
import lombok.Setter;

import java.io.Serializable;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author liyuncong
 * @version 1.0
 * @file PortalCpDefaultConfigImpl
 * @brief PortalCpDefaultConfigImpl
 * @details PortalCpDefaultConfigImpl
 * @date 2024-06-07
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2024-06-07               liyuncong          Created
 */

@Setter
public class CpDefaultConfigImpl implements CpConfigStorage, Serializable {

    protected volatile String accessToken;

    protected transient Lock accessTokenLock = new ReentrantLock();

    protected volatile Integer agentId;

    private volatile String corpId;
    private volatile String corpSecret;
    private volatile String token;
    private volatile String aesKey;
    private volatile long expiresTime;

    private volatile String httpProxyHost;
    private volatile int httpProxyPort;
    private volatile String httpProxyUsername;
    private volatile String httpProxyPassword;


    private transient volatile ApacheHttpClientBuilder apacheHttpClientBuilder;

    private volatile String baseApiUrl;


    @Override
    public void setBaseApiUrl(String baseUrl) {
        this.baseApiUrl = baseUrl;
    }

    @Override
    public String getApiUrl(String path) {
        if (baseApiUrl == null) {
            baseApiUrl = CpApiPathConstants.DEFAULT_CP_BASE_URL;
        }
        return baseApiUrl + path;
    }

    @Override
    public String getAccessToken() {
        return this.accessToken;
    }

    @Override
    public Lock getAccessTokenLock() {
        return this.accessTokenLock;
    }

    @Override
    public boolean isAccessTokenExpired() {
        return System.currentTimeMillis() > this.expiresTime;
    }

    @Override
    public void expireAccessToken() {
        this.expiresTime = 0;
    }

    @Override
    public void updateAccessToken(AccessToken accessToken) {
        updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
    }

    @Override
    public void updateAccessToken(String accessToken, int expiresInSeconds) {
        this.accessToken = accessToken;
        this.expiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L;
    }

    @Override
    public boolean autoRefreshToken() {
        return true;
    }

    @Override
    public String getCorpId() {
        return this.corpId;
    }

    @Override
    public String getCorpSecret() {
        return this.corpSecret;
    }

    @Override
    public Integer getAgentId() {
        return this.agentId;
    }

    @Override
    public String getToken() {
        return this.token;
    }

    @Override
    public String getAesKey() {
        return this.aesKey;
    }

    @Override
    public ApacheHttpClientBuilder getApacheHttpClientBuilder() {
        return this.apacheHttpClientBuilder;
    }

    @Override
    public String getHttpProxyHost() {
        return this.httpProxyHost;
    }

    @Override
    public int getHttpProxyPort() {
        return this.httpProxyPort;
    }

    @Override
    public String getHttpProxyUsername() {
        return this.httpProxyUsername;
    }

    @Override
    public String getHttpProxyPassword() {
        return this.httpProxyPassword;
    }

}
